<template>
  <div class="login-container">
    <el-card class="login-form">
      <!-- 图像区 -->
      <div class="logo">
        <img src="@/assets/logo.png" />
      </div>

      <!-- 表单区 -->
      <el-form
        class="loginArea"
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
      >
        <el-form-item prop="username">
          <el-input v-model="ruleForm.username" type="text">
            <i
              class="iconfont icon-user
"
              slot="prefix"
            ></i
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="ruleForm.password" type="password">
            <i class="iconfont icon-3702mima" slot="prefix"></i
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="end">
            <el-button type="primary" @click="submitForm('ruleForm')"
              >登录</el-button
            >
            <el-button @click="resetForm('ruleForm')" type="info"
              >重置</el-button
            ></el-row
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'login',
  data() {
    return {
      ruleForm: {
        username: 'admin',
        password: '123456'
      },
      userinfo: {},
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 5,
            message: '用户名长度在 3 到 5 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '密码长度在 6 到 15 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          try {
            const { data: res } = await this.$http({
              method: 'post',
              url: '/login',
              data: this.ruleForm
            })
            // console.log(res)
            this.userinfo = res.data
            window.sessionStorage.setItem('token', this.userinfo.token)
            this.$router.push('/home')
          } catch (err) {
            return this.$message.error('登录失败' + err)
          }
        } else {
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style>
.login-container {
  height: 100%;
  background-color: #2b5b6b;
}

.login-form {
  width: 450px;
  height: 300px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  overflow: unset;
}

.logo {
  width: 100px;
  height: 100px;
  border: 1px solid #eee;
  box-shadow: 0 0 10px #ddd;
  border-radius: 50%;
  background-color: white;
  padding: 10px;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
}

.logo img {
  height: 100%;
  background-color: #eeeeee;
  border-radius: 50%;
}

.loginArea {
  margin-top: 84px;
}
</style>
